import React from 'react';
import { Form, Input, Button, Checkbox } from 'antd';
import axios from 'axios';

const Login = (props) => {
  const onFinish = (values) => {
    let param = new URLSearchParams;
    param.append("account", values.username);
    param.append("pw", values.password);
    axios(
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: "post",
        url: "http://www.young1024.com:1717/v1/user/login",
        data: param
      }).then(
        (res) => {
          console.log(res);
          localStorage.setItem("user", JSON.stringify(res.data.data.user))
          localStorage.setItem("token", JSON.stringify(res.data.data.token))
          
        //   setTimeout(() => {
        //     props.history.replace("/")
        // }, 1500)
        },
        (err) => { console.log(err); }
      )
  };

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <div style={{ paddingTop: 100, height: "100vh" }}>
      <Form
        name="basic"
        labelCol={{
          span: 8,
        }}
        wrapperCol={{
          span: 8,
        }}
        initialValues={{
          remember: true,
        }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item
          label="用户名"
          name="username"
          rules={[
            {
              required: true,
              message: 'Please input your username!',
            },
          ]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="密码"
          name="password"
          rules={[
            {
              required: true,
              message: 'Please input your password!',
            },
          ]}
        >
          <Input.Password />
        </Form.Item>
{/* 
        <Form.Item
          name="remember"
          valuePropName="checked"
          wrapperCol={{
            offset: 8,
            span: 8,
          }}
        >
          <Checkbox>自动登录</Checkbox>
        </Form.Item> */}

        <Form.Item
          wrapperCol={{
            offset: 8,
            span: 8,
          }}
        >
          <Button type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>
    </div>
  );

}

export default Login;


